package drds.common.db.xml_mapping.mapping;

import drds.common.db.xml_mapping.parameter_and_result_value_type.JdbcType;
import drds.common.db.xml_mapping.parameter_and_result_value_type.TypeHandler;
import drds.common.db.xml_mapping.session.Configuration;


public class ParameterMapping {

  private Configuration configuration;

  private String propertyName;

  private Class<?> javaType = Object.class;
  private JdbcType jdbcType;


  private String jdbcTypeName;
  private String expression;

  private ParameterMapping() {
  }

  public String getPropertyName() {
    return propertyName;
  }

  public Class<?> getJavaType() {
    return javaType;
  }

  /**
   * Used in the UnknownTypeHandler in case there is no handler for the propertyName parameter_and_result_value_type.
   *
   * @return
   */
  public JdbcType getJdbcType() {
    return jdbcType;
  }


  /**
   * Used for handling output of callable statements.
   *
   * @return
   */
  public String getJdbcTypeName() {
    return jdbcTypeName;
  }

  /**
   * Not used
   *
   * @return
   */
  public String getExpression() {
    return expression;
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder("ParameterMapping{");
    //sb.append("configuration=").append(configuration); // configuration doesn't have a useful .toString()
    sb.append("propertyName='").append(propertyName).append('\'');

    sb.append(", javaType=").append(javaType);
    sb.append(", jdbcType=").append(jdbcType);

    //sb.append(", typeHandler=").append(typeHandler); // typeHandler also doesn't have a useful .toString()

    sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
    sb.append(", expression='").append(expression).append('\'');
    sb.append('}');
    return sb.toString();
  }

  public static class Builder {
    private ParameterMapping parameterMapping = new ParameterMapping();

    public Builder(Configuration configuration, String property, TypeHandler<?> typeHandler) {
      parameterMapping.configuration = configuration;
      parameterMapping.propertyName = property;


    }

    public Builder(Configuration configuration, String property, Class<?> javaType) {
      parameterMapping.configuration = configuration;
      parameterMapping.propertyName = property;
      parameterMapping.javaType = javaType;

    }


    public Builder javaType(Class<?> javaType) {
      parameterMapping.javaType = javaType;
      return this;
    }

    public Builder jdbcType(JdbcType jdbcType) {
      parameterMapping.jdbcType = jdbcType;
      return this;
    }


    public Builder jdbcTypeName(String jdbcTypeName) {
      parameterMapping.jdbcTypeName = jdbcTypeName;
      return this;
    }

    public Builder expression(String expression) {
      parameterMapping.expression = expression;
      return this;
    }

    public ParameterMapping build() {


      return parameterMapping;
    }


  }
}
